package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.Activity.BillDataOauthActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.OutputProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/25.
 */

public class OutputAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<OutputBill> outputBillList;
    private Context mContext;
    private int position;

    public OutputAdapter(List<OutputBill> list, Context context) {
        this.outputBillList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (outputBillList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.out_bill_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            this.position = position;
            viewHolder.BindViewData(outputBillList.get(position));
            viewHolder.setViewListener(outputBillList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (outputBillList.size() > 0) {
            return outputBillList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        TextView billNumberTV;
        TextView outTimeTV;
        TextView netWeightTV;
        TextView grossWeightTV;
        TextView tareWeightTV;
        TextView receviedGossWeightTV;
        TextView receviedTareWeightTV;
        TextView receviedNetWeightTV;
        TextView receviedLossWeightTV;
        TextView receviedDeductWeightTV;
        TextView receviedStatusTv;
        LinearLayout receviedLinerLayout;
        TextView coalNameTV;
        TextView companyTV;
        TextView customerCompanyTV;
        TextView carInformationTV;
        TextView sortTV;
        TextView remarkTV;
        Button oauthBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            billNumberTV = (TextView) itemView.findViewById(R.id.bill_number);
            outTimeTV = (TextView) itemView.findViewById(R.id.out_time);
            netWeightTV = (TextView) itemView.findViewById(R.id.net_weight);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            companyTV = (TextView) itemView.findViewById(R.id.company);
            carInformationTV = (TextView) itemView.findViewById(R.id.car_information);
            sortTV = (TextView) itemView.findViewById(R.id.sort);
            remarkTV = (TextView) itemView.findViewById(R.id.remark);
            grossWeightTV = (TextView) itemView.findViewById(R.id.gross_weight);
            tareWeightTV = (TextView) itemView.findViewById(R.id.tare_weight);
            receviedGossWeightTV = (TextView) itemView.findViewById(R.id.received_gross_weight);
            receviedTareWeightTV = (TextView) itemView.findViewById(R.id.received_tare_weight);
            receviedNetWeightTV = (TextView) itemView.findViewById(R.id.received_net_weight);
            receviedLossWeightTV = (TextView) itemView.findViewById(R.id.received_loss_weight);
            receviedDeductWeightTV = (TextView) itemView.findViewById(R.id.received_deduct_weight);
            receviedStatusTv = (TextView) itemView.findViewById(R.id.received_status);
            customerCompanyTV = (TextView) itemView.findViewById(R.id.customer_company);
            receviedLinerLayout = (LinearLayout) itemView.findViewById(R.id.received_linearLayout);
            oauthBtn = (Button) itemView.findViewById(R.id.oauth_btn);
        }

        public void BindViewData(OutputBill bill) {
            billNumberTV.setText(bill.getObnum());
            customerCompanyTV.setText(bill.getSendcompany());
            sortTV.setText((position + 1) + "");
            if (bill.getIsaudit() == 1) {
                oauthBtn.setVisibility(View.GONE);
            } else {
                oauthBtn.setVisibility(View.VISIBLE);
            }
            if (bill.getOuttime() != null) {
                outTimeTV.setText(TimeUtils.parseDate(bill.getOuttime()));
            } else {
                outTimeTV.setText(TimeUtils.parseDate(bill.getAddtime()));
            }
            grossWeightTV.setText("毛重" + bill.getSendgrossweight());
            tareWeightTV.setText("皮重" + bill.getSendtareweight());
            netWeightTV.setText("净重" + bill.getSendnetweight());
            coalNameTV.setText(bill.getCoaltype());
            companyTV.setText(bill.getBuycompany());
            carInformationTV.setText(bill.getCarnumber()
                    + "  " + bill.getCarowner() + "  "
                    + BaseActivity.formatPhoneNumber(bill.getCarconnectphone()));
            if (bill.getIsreceived() == 1) {
                receviedStatusTv.setText("已经到货 时间：" + TimeUtils.parseDateTime(bill.getReceivedtime()));
                receviedLinerLayout.setVisibility(View.VISIBLE);
                receviedGossWeightTV.setText("毛重" + bill.getInputgrossweight());
                receviedTareWeightTV.setText("皮重" + bill.getInputtareweight());
                receviedTareWeightTV.setText("净重" + bill.getInputnetweight());
                receviedTareWeightTV.setText("磅差" + bill.getLossweight());
                receviedTareWeightTV.setText("扣重" + bill.getDeductweight());
            } else {
                receviedStatusTv.setText("未到货");
                receviedLinerLayout.setVisibility(View.GONE);
            }
            if (bill.getRemark() != null && bill.getRemark() != "Null" && bill.getRemark().length() > 2) {
                remarkTV.setVisibility(View.VISIBLE);
                remarkTV.setText("备注：" + bill.getRemark());
            } else {
                remarkTV.setVisibility(View.GONE);
            }
        }

        public void setViewListener(final OutputBill bill) {
            oauthBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.oauth_btn:
                            OutputProvide provide = new OutputProvide(mContext, new UpdateListViewListener() {
                                @Override
                                public void onRefreshData(Object result) {

                                }

                                @Override
                                public void onLoadMoreData(Object result) {

                                }

                                @Override
                                public void showMessage(String msg) {
                                    if (msg == "ok") {
                                        outputBillList.remove(bill);
                                        notifyDataSetChanged();
                                        ToastUtils.showLong("审核成功");
                                    } else if (msg.contains("failure")) {
                                        ToastUtils.showLong("审核失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()));
                                    } else {
                                        ToastUtils.showLong(msg);
                                    }
                                }

                                @Override
                                public void showProgress() {
                                    BillDataOauthActivity activity = (BillDataOauthActivity) mContext;
                                    activity.getSweetAlertDialog(activity).showDialog("", 1200);
                                }

                                @Override
                                public void hideProgress() {
                                    BillDataOauthActivity activity = (BillDataOauthActivity) mContext;
                                    activity.getSweetAlertDialog(activity).dismiss();
                                }
                            });
                            provide.oauth((int) bill.getId());
                            break;
                    }
                }
            });
        }
    }
}
